package com.guigu.servicegoods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.servicegoods.domain.TbOrderItem;
import com.guigu.servicegoods.domain.TbStockBack;
import com.guigu.servicegoods.mapper.TbSkuMapper;
import com.guigu.servicegoods.mapper.TbStockBackMapper;
import com.guigu.servicegoods.service.TbSkuService;
import com.guigu.servicegoods.service.TbStockBackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbStockBackServiceImpl extends ServiceImpl<TbStockBackMapper, TbStockBack> implements TbStockBackService {

    @Autowired
    private TbSkuMapper skuMapper;

    @Override
    public Result findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        QueryWrapper<TbStockBack> wrapper = createExample(searchMap);
        Page<TbStockBack> pageStockBack = new Page<>(page,size);
        baseMapper.selectPage(pageStockBack,wrapper);
        return Result.ok().data("row",pageStockBack.getRecords()).data("total",pageStockBack.getTotal());
    }

    @Override
    public void doBack() {
        System.out.println("库存回滚任务开始");
        //查询库存回滚表中状态为0的记录
        QueryWrapper<TbStockBack> wrapper = new QueryWrapper<>();
        wrapper.eq("status","0");
        List<TbStockBack> stockBackList = baseMapper.selectList(wrapper);
        for(TbStockBack stockBack:stockBackList){
            //添加库存
            skuMapper.deductionStock(stockBack.getSkuId(), - stockBack.getNum() );
            //减少销量
            skuMapper.addSaleNum(stockBack.getSkuId(), -stockBack.getNum());
            stockBack.setStatus("1");
            stockBack.setBackTime(new Date());
            baseMapper.updateById(stockBack);
        }
        System.out.println("库存回滚任务结束");
    }


    @RabbitListener(queues = "queue.skuback")
    public void addList(Message message) {
        String jsonString = new String(message.getBody());
        List<TbOrderItem> orderItems = JSON.parseArray(jsonString, TbOrderItem.class);
        for(TbOrderItem orderItem:orderItems){
            TbStockBack stockBack=new TbStockBack();
            stockBack.setOrderId(orderItem.getOrderId());
            stockBack.setSkuId(orderItem.getSkuId());
            stockBack.setStatus("0");
            stockBack.setNum(orderItem.getNum());
            stockBack.setCreateTime(new Date());
            baseMapper.insert(stockBack);
        }
    }

    // 根据查询条件封装模块对象
    private QueryWrapper<TbStockBack> createExample(Map<String, Object> searchMap) {
        QueryWrapper<TbStockBack> wrapper = new QueryWrapper<>();
        if(searchMap!=null) {
            if(searchMap.get("orderId")!=null)
                wrapper.eq("order_id",searchMap.get("orderId"));
            if(searchMap.get("skuId")!=null && !"".equals(searchMap.get("skuId")))
                wrapper.like("sku_id",searchMap.get("skuId"));
        }
        return wrapper;
    }
}
